package com.shiguo365.market.display.activity;


import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.adapter.ConfirmAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IConfirmView;
import com.shiguo365.market.presenter.contract.IConfirmPresenter;
import com.shiguo365.market.presenter.implement.ConfirmPresenter;
import com.shiguo365.market.utils.HandlerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConfirmActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,IConfirmView {

    private String TAG="ConfirmActivity";

    private LinearLayoutManager mLayoutManager;

    ConfirmAdapter mAdapter;

    private IConfirmPresenter mConfirmPresenter;

    @BindView(R.id.buttonConfirm) Button mBtnConfirm;

    @BindView(R.id.textViewAddress) TextView mTvAddress;

    @BindView(R.id.textViewAddress2) TextView mTvAddress2;

    @BindView(R.id.etConfirmContent) EditText mEdtConfirmContent;


    private List<Object> jsonObjectList= new ArrayList<>();;

    @BindView(R.id.confirm_list_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.my_recycler_view_confirm)
    protected RecyclerView mRecyclerView;
    @BindView(R.id.confirm_list_toolbar)
    protected Toolbar toolbar;

    protected int uid = 1;
    protected int aid = 1;
    protected String finalAmount = "";
    protected String min_amount = "";
    protected String remark = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"test1:onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm);

        ButterKnife.bind(this); //ButterKnife初始化

        mConfirmPresenter=new ConfirmPresenter(this,this);
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        Log.d(TAG,"test3:"+jsonObjectList);
        mAdapter = new ConfirmAdapter(mConfirmPresenter,jsonObjectList);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
//创建并设置Adapter
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);

        mBtnConfirm.bringToFront();
    }

    @Override
    public void onRefresh() {
        Log.d(TAG,"test2:onRefresh");
        //添加刷新代码
        jsonObjectList.clear();

        uid = MyApplication.getUID();
        mConfirmPresenter.getConfirmByUI(uid);

    }

    @Override
    public void OnGetConfirmOK(JSONArray jsonarray) {

        for (int i = 0; i < jsonarray.size(); i++) {
            this.jsonObjectList.add(jsonarray.get(i));
        }

        mAdapter.notifyDataSetChanged();

        Log.d("test",jsonObjectList.toString());
    }

    @Override
    public void OnGetInfoOK(JSONObject result) {

        finalAmount = result.getString("finalAmount");
        JSONObject address = result.getJSONObject("address");
        min_amount = result.getString("min_amount");



        if(Double.parseDouble(finalAmount) < Double.parseDouble(min_amount)){
            mBtnConfirm.setClickable(false);
            mBtnConfirm.setText("订单金额不足"+min_amount+"不好下单");
        }else{
            mBtnConfirm.setClickable(true);
            mBtnConfirm.setText("提交订单(￥"+finalAmount+")");
        }

        if(address.isEmpty()){
            Intent intent = new Intent(ConfirmActivity.this,AddressAddActivity.class);
            startActivity(intent);
            finish();
        }

        aid = address.getIntValue("id");

        String name = address.getString("name");
        String phone = address.getString("phone");
        String street = address.getString("street");

        String str = name+" ("+phone+")";

        mTvAddress.setText(str);
        mTvAddress2.setText(street);

    }

    @Override
    public void OnGetConfirmFinsh() {
        refreshLayout.setRefreshing(false);
    }


    @OnClick(R.id.buttonConfirm)
    public void addOrder(){

        remark = mEdtConfirmContent.getText().toString();

        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
        builder.setTitle("下单确认"); //设置标题
        builder.setMessage("您确定要下单吗?"); //设置内容
        builder.setIcon(R.mipmap.ic_launcher);//设置图标，图片id即可
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mConfirmPresenter.addOrder(uid,aid,finalAmount,remark);

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });


        //参数都设置完成了，创建并显示出来
        builder.create().show();

    }

    //显示消息
    @Override
    public void showConfirmMessages(String msg) {
        ToastUtils.with(this).show(msg);
    }

    //创建订单成功后进入订单列表页面
    @Override
    public void confirmSuccess() {
        Intent intent = new Intent(ConfirmActivity.this,OrderActivity.class);
        startActivity(intent);
        finish();
    }

    @OnClick(R.id.relativeLayoutConfirm)
    protected void onBtnAddressClick(View view){
        Intent intent = new Intent(ConfirmActivity.this,AddressListActivity.class);
        intent.putExtra("mold","FromOrderConfirm");
        startActivityForResult(intent,1);
        //finish();
    }

    //地址选择后返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
        if(requestCode==RESULT_OK){
            RefreshUtils.refreshOnCreate(refreshLayout, this);
        }

    }

}
